import 'package:flutter/cupertino.dart';
import 'package:get/get.dart';
import 'package:get/get_core/src/get_main.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';
import 'package:qianyi/controller/homeTabController.dart';
import 'package:qianyi/entity/message/Message.dart';
import 'package:qianyi/entity/message/MessageChat.dart';
import 'package:qianyi/state/token.dart';

import '../../network/http_config.dart';
import '../../network/http_entity.dart';
import '../../network/http_manager.dart';
import '../../ui/widget/custom_refresh_mixin.dart';

class MessageChatViewModel extends ChangeNotifier with CustomRefreshMixin{
  HomeTabController homeTabController = Get.find();
  TextEditingController introduceController = TextEditingController();
  TokenController tokenController = Get.find();
  RefreshController refreshController = RefreshController(initialRefresh: true);

  @override
  Future<List> requestList(int page) async {
    List tempL = [];
    if(hasNext == true || page == 1) {
      if(page == 2) {
        page = page - 1;
      }

      Map<String, dynamic> params = {};

      // String id = UserManager.userEntity!.userInfo!.id!;
      homeTabController.messageItem!.sendId;
      String uid = await tokenController.getUid();
      params = {
        "sendUid":homeTabController.messageItem!.sendId,
        "acceptUid":uid
      };


      Map<String,dynamic> param2 = {"uid":uid};
      params["uid"] = await tokenController.getUid();;
      Map<String, dynamic> params3 = {
        "sendId":uid,
        "acceptId":homeTabController.messageItem!.sendId
      };
      // HttpEntity? result1 = await HttpManager.getInstance().request(url: '/platform/chat/updateRecordCount',method: Method.get,params: params3) as HttpEntity;
      HttpEntity? result = await HttpManager.getInstance().request(url: "/platform/chat/getChatRecord/${page}/20", params: params,method: Method.get) as HttpEntity;
      if(result.oriData['data'] == null) {
        return tempL;
      }
      MessageRecordChatList listResult = MessageRecordChatList.fromJson(result.getOriData()["data"]["records"],int.parse(result.getOriData()['data']['total']));
      List<MessageChatRecord>? temps = listResult!.records; // 这里不需要强制类型转换
      if(page*20 >= listResult.total) {
        hasNext = false;
      }
      if (temps != null) {
        tempL.addAll(temps);
      }
      if (temps.length == 0 || temps == null) {
        return tempL;
      }

    } else {

    }


    return tempL;
  }
  @override
  MessageChatViewModel() {
    this.onLoading();
  }

  @override
  void onRequestComplete() {
    notifyListeners();
  }

  void onrefreshXt() {
    this.onRefresh();
    notifyListeners();
  }

}